/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tygzs.minerpg.Configuration.GuildConfig;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.tygzs.minerpg.Main;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

/**
 *
 * @author thtTNT
 */
public class GuildConfig
{
    
    private Map<String, GuildInfo> guilds = new HashMap<>();
    
    public GuildConfig()
    {
        File file = new File(Main.getPlugin().getDataFolder(), "Guild");
        if (!file.exists())
        {
            file.mkdirs();
            return;
        }
        File[] files = file.listFiles();
        for (File DataFile : files)
        {
            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
            String name = config.getString("name");
            List<String> members = config.getStringList("memebers");
            this.guilds.put(name, new GuildInfo(name, members));
        }
    }
    
    public void save()
    {
        try
        {
            File file = new File(Main.getPlugin().getDataFolder(), "Guild");
            file.delete();
            file.mkdirs();
            for (String name : this.guilds.keySet())
            {
                file = new File(file, name + ".yml");
                file.createNewFile();
                FileConfiguration config = YamlConfiguration.loadConfiguration(file);
                config.set("name", name);
                config.set("members", this.guilds.get(name).getMembers());
                config.save(file);
            }
        } catch (IOException ex)
        {
            Logger.getLogger(GuildConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
